import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../view/Login/login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: Login,
  },
  {
    path: '/home',
    component: () => import('../view/Home/home.vue'),
    redirect: '/home/users',
    children: [
      {
        path: '/home/users',
        component: () => import('../view/Users/users.vue'),
      },
      {
        path: '/home/roles',
        component: () => import('../view/Roles/roles.vue'),
      },{
        path: '/home/rights',
        component: () => import('../view/Rights/rights.vue'),
      },{
        path: '/home/goods',
        component: () => import('../view/Goods/goods.vue'),
      },{
        path: '/home/addGoods',
        component: () => import('../view/Goods/addGoods.vue'),
      },{
        path: '/home/categories',
        component: () => import('../view/Categories/categories.vue'),
      },{
        path:'/home/params',
        component:()=>import('../view/Params/params.vue')
      },{
        path:'/home/orders',
        component:()=>import('../view/Order/order.vue')
      },{
        path:'/home/reports',
        component:()=>import('../view/Reports/reports.vue')
      }
    ],
  },
]

const router = new VueRouter({
  routes,
})

router.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem('token')
  
  if (token || to.path === '/login') {
    next()
  }
})

export default router
